package news.web;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import news.model.Admin;
import news.model.CommonResult;
import news.model.Role;
import news.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员管理")
@RestController
@RequestMapping(path = "/admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @ApiOperation(value = "查询管理员信息")
    @PostMapping(path = "/admins")
    public CommonResult admins(@RequestBody  Admin admin){

        PageInfo<Admin> admins = adminService.adminList(admin);
        CommonResult commonResult = new CommonResult(200,admins,"查询成功");
        return commonResult;
    }


    @ApiOperation(value = "查询角色信息")
    @GetMapping(path = "roles")
    public CommonResult roles(){
        List<Role> roleList = adminService.roles();
        CommonResult commonResult = new CommonResult(200,roleList,"查询成功");
        return commonResult;
    }

    @ApiOperation(value = "保存管理员信息")
    @PostMapping(path = "/save")
    public CommonResult save(@RequestBody Admin admin,@RequestHeader String adminToken){
        if(admin.getId() == null){
            adminService.save(admin,adminToken);
        }else{
            adminService.update(admin,adminToken);
        }

        CommonResult commonResult = new CommonResult(200,"保存成功");
        return commonResult;
    }

    @ApiOperation(value = "删除管理员")
    @GetMapping(path = "/delete")
    public CommonResult delete(Integer id){
        adminService.delete(id);
        CommonResult commonResult = new CommonResult(200,"删除成功");
        return commonResult;
    }

    @ApiOperation(value = "通过ID查询管理员")
    @GetMapping(path = "/findAdminById")
    public CommonResult findAdminById(Integer id){
        Admin admin = adminService.findAdminById(id);
        CommonResult commonResult = new CommonResult(200,admin,"查询成功");
        return commonResult;
    }

    //修改密码

    public CommonResult changePwd(String password){
        
        adminService.changePwd(password);
        return new CommonResult(200,"修改密码成功");
    }
}
